package com.atom.common.remote.file;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atom.common.CommonConstant;
import com.atom.common.dao.FileDao;
import com.atom.common.entity.FileDO;
import com.atom.common.exception.AvensRemoteDatabaseDaoException;
import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.CharSetUtil;
import com.atom.common.util.GlobalConfigReader;
import com.atom.common.util.HttpUtil;
import com.atom.common.util.JsonResultParser;
import com.atom.common.util.TicketUtil;

@Service
public class RemoteFileDao {

  Logger logger = Logger.getLogger(RemoteFileDao.class);

  @Autowired(required = false) HttpSession session;
  @Resource FileDao fileDao;

  private static final String HOST = GlobalConfigReader.getStringValue(CommonConstant.DATAMANAGE_SERVERNAME);
  private static final String BUCKET_NAME = GlobalConfigReader.getStringValue(CommonConstant.BUCKET_NAME);
  private static final String APP_ID = GlobalConfigReader.getStringValue(CommonConstant.APP_ID);

  public void delete(String key) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "SQLOperationService.deleteLogicalRecord.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      params.put("bucketName", BUCKET_NAME);
      params.put("objectName", key);
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
      fileDao.deleteFile(key);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Delete Remote File Failed key : " + key);
    }
  }

  public void deleteWithoutLocal(String key) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "SQLOperationService.deleteLogicalRecord.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      params.put("bucketName", BUCKET_NAME);
      params.put("objectName", key);
      String result = CharSetUtil.decodeUnicode(HttpUtil.post(finalUrl, params));
      JsonResultParser.parse(result);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Delete Remote File Failed key : " + key);
    }
  }


  public void add(int fileType, String taskId, String processInstanceId, String fileName, 
      String key, String fileSize ,UserUtil user) {
    fileDao.addFile(fileType, taskId, processInstanceId, fileName, key, fileSize, user);
  }

  public List<FileDO> find(String processInstanceId, int fileType) {
    return fileDao.findByTypeByProcessInstanceId(fileType, processInstanceId);
  }

  public String download(String key) {
    Map<String, String> params = new HashMap<String, String>();
    final String METHOD = "LogicalObjectService.download.do";
    StringBuffer url = new StringBuffer(HOST);
    url.append(METHOD);    
    try {
      String finalUrl = TicketUtil.appendTicket(url, session);
      params.put("app_id", APP_ID);
      params.put("bucket_name", BUCKET_NAME);
      params.put("object_key", key);
      String result = CharSetUtil.decodeUnicode(HttpUtil.get(finalUrl, params));
      return result;    
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
      throw new AvensRemoteDatabaseDaoException("Download Remote File Failed key : " + key);
    }
  }

}
